#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/sysctl.h"
#include "driverlib/adc.h"

int main(void)
{
	uint32_t ui32ADC0Value[4]; // Array of unsigned 32 bit int
	volatile uint32_t ui32TempAvg; ////// Average temperature value
	volatile uint32_t ui32TempValueC; ///// C temperature
	volatile uint32_t ui32TempValueF; ////// F temperature

	SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ); // System clock of 400MHz/5 = 80MHz

	SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0); ////// Enables the ADC0

	ADCSequenceConfigure(ADC0_BASE, 1, ADC_TRIGGER_PROCESSOR, 0); /////// Configures the triger source. Set for processor trigger.
	ADCSequenceStepConfigure(ADC0_BASE, 1, 0, ADC_CTL_TS); // Configures the step sequencer. Enables data0
	ADCSequenceStepConfigure(ADC0_BASE, 1, 1, ADC_CTL_TS); // Configures the step sequencer. Enables data1
	ADCSequenceStepConfigure(ADC0_BASE, 1, 2, ADC_CTL_TS); // Configures the step sequencer. Enables data2
	ADCSequenceStepConfigure(ADC0_BASE,1,3,ADC_CTL_TS|ADC_CTL_IE|ADC_CTL_END); // Configures the step sequencer. Causes interrupt when done Enables data3
	ADCSequenceEnable(ADC0_BASE, 1); /// Enables the sequencer

	while(1)
	{
	   ADCIntClear(ADC0_BASE, 1); /////// Clear the interrupt from the ADC.
	   ADCProcessorTrigger(ADC0_BASE, 1); ////// Starts the sample sequencer

	   while(!ADCIntStatus(ADC0_BASE, 1, false)) ////// Stalls until it gets its ADC sample back
	   {
	   }

	   ADCSequenceDataGet(ADC0_BASE, 1, ui32ADC0Value); /// Gets the data acquired
	   ui32TempAvg = (ui32ADC0Value[0] + ui32ADC0Value[1] + ui32ADC0Value[2] + ui32ADC0Value[3] + 2)/4; /// Averages
	   ui32TempValueC = (1475 - ((2475 * ui32TempAvg)) / 4096)/10; /// Celsius
	   ui32TempValueF = ((ui32TempValueC * 9) + 160) / 5; ///// Farenheit
	}
}
